/*
* @Author: Choisaaaa
* @Date: 2018/5/7  22:55
*  用户头像管理
*/


require("./user-img-center.css");
require("page/common/nav/nav.js");
require("page/common/header/header.js");
var navSide = require("page/common/nav-side/nav-side.js");
var _uploadImage = require("service/img-upload-service.js");
var _mm = require("util/mm.js");
var _user = require("service/user-service.js");
var templateIndex = require("./user-img-center.string");

var page = {

    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    //初始化加载的信息
    onLoad : function(){
        navSide.init({//加载侧边导航
            name : "user-img-center",
        });
        this.loadUserImagesInfo();//加载用户信息
    },
    bindEvent : function () {
        this.clickSubmitBtn()
    },
    clickSubmitBtn : function () {
        var _this = this;
        $(document).on("click",".btn-upload", function () {
            var shopImg = $('#shop-img')[0].files[0];
            if(!shopImg){
                _mm.errorTips("请先选择需要上传的文件，再提交");
                return;
            }
            var formData = new FormData();
            formData.append('shopImg', shopImg);

            _uploadImage.uploadImage(formData, function (res) {
                _user.updateImage(res.data[0].url, function (res) { //更新用户头像
                    var userHtml = _mm.renderHtml(templateIndex,res.data);
                    $(".img-center-con").html(userHtml);
                }, function (err) {
                    _mm.errorTips(err.msg);
                });
            }, function (res) {
                _mm.errorTips(res.msg);
            });
        });
    },
    loadUserImagesInfo : function(){
        var userHtml = "",
            userImageHtml="";

        _user.getUserInfo(
            function(res){//请求成功时的回调函数
                userHtml = _mm.renderHtml(templateIndex,res.data);
                $(".img-center-con").html(userHtml);
                //userImageHtml = _mm.renderHtml(templateUserImage,res.data);
               // $(".user-image").html(userImageHtml);
            },
            function(err){//请求失败时的回调函数
                _mm.errorTips(err.msg);
            }
        );
    }

};
$(function(){
    page.init();
});

